
// https://blog.csdn.net/dora_310/article/details/52863422 【针对集合的操作】

// http://docs.groovy-lang.org/next/html/documentation/working-with-collections.html  【比较全面】
//list排序
def xxx(){
	  def arr4=[8,6,5,1,9,3]
	  print arr4.getClass()
	  println arr4.sort()
	  //list打乱元素顺序
	  println arr4.sort{
		  Math.random()
	  }
	  //取得对应元素的索引
	  println arr4.indexOf(5)
	  //list求和
	  println arr4.sum()
	  //取出最大值和最小值
	  println arr4.max()
}

def grep(){
      // 过滤
    def menus = [1,2,3,4,5].grep{
        it > 1
    }   

    println menus
}

// grep() ; return ;

import groovy.json.JsonSlurper
def item(){
	def jsonSlpuer = new JsonSlurper()
	def versionObjs = jsonSlpuer.parse(new File(System.getProperty("user.dir") + "/json.json"))
	for (item in versionObjs.data) {
		//println item.version
	}
	// 很方便，直接在item上操作
	println versionObjs.data[1..10].version
	println versionObjs.data[1..10].size()
	println versionObjs.data.size()	

	print versionObjs.data[1..10].select()

	// 从第7个, 取到最后一个
	println versionObjs.data[7..-1].size()
}

def collect(){
	 def lst = [1,2,3,4];
      def newlst = [];
      newlst = lst.collect ({element -> return element * element})
      println(newlst);
}

def filter(){
	
	 def lst = [1,2,3,4];
      def newlst = [];
      newlst = lst.findAll({el -> el >= 3 })
      println(newlst);

}

def check(){
	println 1 in [1,2,3]
}

def findAll(){
	[1,2,3].findAll({it >=2}).each { 
		println it
	}
}

def foreach_java(){
	for (int i = 0; i < 5; i++) {
		print i
	}
}

def foreach_in(){
	for ( i in 0..9 ) {
		print i
	}

	for (i in (0..4).toArray()){
		print i	
	}
}

def foreach_map(){
	def map = ['abc':1, 'def':2, 'xyz':3]
	for ( e in map ) {
		println "==========="
		println e.key
		println e.value
		
	}

	for ( v in map.values() ) {
		print v
	}

}

def define_map(){
	def mapping = ["bladex-boot-qd":"trunk", 
	"bladex-boot-all":"trunk-2.5.1-all", 
	"bladex-easyopen":"trunk-easyopen", 
	"blade-xxljob-admin":"trunk-blade-xxljob-admin",
	"blade-camel":"trunk-camel"]
	print(mapping["bladex-boot-qd"])	
}

def printMap(Map map){
	for ( e in map ) {
		println "==========="
		println e.key
		println e.value
		
	}
}

def sorted(lst){
	result = lst.toSorted { a, b -> Integer.valueOf(a.length()).compareTo(b.length()) }	
	print result
}

//collect()
//filter()
//check()
//define_map()
// ==========
//findAll()
//foreach_map()

//printMap(['abc':1, 'def':2, 'xyz':3])

//sorted(['333', '1', '4444', '22'])


println([1,2] + 3)


println([1,2].join("\r\n"))

println("${[1,2].size()}")